package com.bestcem.xm.qdes.controller.v3;

import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.common.core.annotation.XmGet;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.component.security.context.SecurityContextHolder;
import com.bestcem.xm.qdes.controller.param.GetFolderParam;
import com.bestcem.xm.qdes.controller.param.ProjectFolderIdParam;
import com.bestcem.xm.qdes.controller.param.ProjectFolderParam;
import com.bestcem.xm.qdes.controller.param.ProjectFolderUpdateParam;
import com.bestcem.xm.qdes.service.QdesProjectFolderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "问卷文件夹编辑-V3")
@RestController
@RequiredArgsConstructor
@RequestMapping("/qdes/v3/projects")
public class ProjectFolderController {
    private @NonNull QdesProjectFolderService projectfolderService;

    @ApiOperation(value = "获取问卷文件夹")
    @GetMapping({"/folders"})
    public XmResultJson<JSONObject> getProjectFolder(@XmGet GetFolderParam getFolderParam) {
        log.info("Now to getProjectFolder");
        JSONObject folderJs = new JSONObject();
        folderJs.put("data", projectfolderService.getFoldertree(getFolderParam));
        return XmResultJson.success(folderJs);
    }

    @ApiOperation(value = "添加问卷文件夹")
    @PostMapping({"/foldertree"})
    public XmResultJson<Void> createProjectFolder(@RequestBody @Validated ProjectFolderParam param) {
        log.info("Now to projectTitle, param = {}", param);
        param.setUserId(SecurityContextHolder.getUserId());
        param.setOrgId(SecurityContextHolder.getOrgId());
        projectfolderService.createFoldertree(param);
        return XmResultJson.success();
    }

    @ApiOperation(value = "修改问卷文件夹名称")
    @PutMapping({"/foldertree"})
    public XmResultJson<Void> updateProjectFolder(@RequestBody @Validated ProjectFolderUpdateParam param) {
        log.info("Now to updateProjectTitle, param = {}", param);
        param.setUserId(SecurityContextHolder.getUserId());
        param.setOrgId(SecurityContextHolder.getOrgId());
        projectfolderService.updateFoldertree(param);
        return XmResultJson.success();
    }

    @ApiOperation(value = "删除问卷文件夹")
    @DeleteMapping({"/foldertree"})
    public XmResultJson<Void> deleteProjectFolder(@RequestBody @Validated ProjectFolderIdParam param) {
        log.info("Now to deleteProjectTitle, param = {}", param);
        param.setUserId(SecurityContextHolder.getUserId());
        param.setOrgId(SecurityContextHolder.getOrgId());
        projectfolderService.deleteFoldertree(param);
        return XmResultJson.success();
    }
}
